<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>login</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="static/css/layui.css" media="all">
</head>
<body bgcolor="#faf5f5">

<ul class="layui-nav layui-bg-cyan">
    <li class="layui-nav-item"><a href="/sports">运动会管理系统</a></li>
</ul>


<div class="layui-container fly-marginTop" STYLE="background-color: white">
    <div class="fly-panel fly-panel-user" pad20="">
        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title">
                <li class="layui-this">登入</li>
            </ul>
            <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form layui-form-pane">
                        <form method="post" action="user/login">

                            <div class="layui-form-item">
                                <label class="layui-form-label">身份</label>
                                <div class="layui-input-inline">
                                    <select name="identity">
                                        <option value="0">学生</option>
                                        <option value="1">管理员</option>
                                    </select>
                                </div>
                            </div>


                            <div class="layui-form-item">
                                <label class="layui-form-label">学号/工号</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="L_loginName" name="loginName"
                                           required="" lay-verify="loginName"
                                           autocomplete="off" class="layui-input"></div>
                            </div>
                            <div class="layui-form-item"><label for="L_pass" class="layui-form-label">密码</label>
                                <div class="layui-input-inline"><input type="password" id="L_pass" name="pwd"
                                                                       required="" lay-verify="pwd"
                                                                       autocomplete="off" class="layui-input"></div>
                                <div class="layui-form-mid layui-word-aux"> 若您没有修改过密码，请使用初始密码（123456）登录</div>
                            </div>
                            <div class="layui-form-item"><label class="layui-form-label">图形码</label>
                                <div class="layui-input-inline"><input type="text" id="L_imagecode" name="imagecode"
                                                                       required="" lay-verify="imagecode"
                                                                       autocomplete="off" class="layui-input"></div>
                                <div class="layui-form-mid" style="padding: 0!important;">
                                    <img th:src="@{/checkcode}" onClick="this.src=this.src+'?'+Math.random();"
                                         class="fly-imagecode"></div>
                                <div class="layui-form-mid layui-word-aux"> 点击图片刷新</div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即登录</button>
                                <span style="padding-left:20px;">
                                    <a th:href="@{/toPage(url=forget)}">忘记密码？</a>
                                </span></div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="div1"></div>


<script th:src="@{static/layui.js}" charset="utf-8"></script>

<script>
    //Demo
    layui.use('form', function () {
        var form = layui.form;
        var $ = layui.jquery;

        //自定义验证规则
        form.verify({
            loginName: function (value) {
                if (value.length < 5) {
                    return '学号/工号至少得5个字符啊';
                }
            }
            , pwd: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , imagecode: function (value) {
                let checkcode = "";
                $.ajax({
                    url: "checkcodetext",
                    type: "GET",
                    async: false,
                    success: function (result) {
                        checkcode = result;
                    }
                });
                if (checkcode != value) {
                    return '验证码输入错误';
                }
            }
        });

        //监听提交
        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: "user/login",
                contentType: "application/json",
                type: 'POST',
                data: JSON.stringify(data.field),
                dataType: 'text',
                async: false,
                cache: false,
                success: function (result) {
                    if (result == "success") {
                        window.location.href = "/sports";
                    } else {
                        layer.msg("用户名或密码错误")
                    }
                },
                error: function (e) {
                    layer.msg("服务器好像出了问题，请稍后再试");
                }
            });
            return false;
        });
    });
</script>

</body>
</html>